#!usr/bin/env python  
#@time: 2018/01/24
#把屏幕设置为 模拟关机状态，哈哈
from tkinter import *
from pykeyboard import PyKeyboard
import time,datetime,sched,threading

class LockScreen:
    def __init__(self):
        self.root = Tk()
        self.w, self.h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()

    def key(self, event):
        print('键盘按键: %s' % event)
        self.root.destroy()

    def lock(self):
        self.root.overrideredirect(True)
        self.root.attributes("-alpha", 1)  # 窗口透明度60 %
        self.root.bind('q', self.key)
        self.canvas = Canvas(self.root, width=self.w, height=self.h, bg="black", highlightthickness=0)
        self.canvas.pack()
        self.root.mainloop()

    def unlock(self):
        key = PyKeyboard()
        key.press_key(key.control_l_key)
        key.tap_key(key.space_key)
        key.release_key(key.control_l_key)
        time.sleep(3)
        key.tap_key('q')


class Timer(object):
    def __init__(self):
        self.schedule = sched.scheduler(time.time,time.sleep)

    def execute_command(self,cmd):
        if cmd == 'lock':
            print('锁定屏幕')
            LockScreen().lock()
        else:
            print('解锁')
            LockScreen().unlock()

    def cmd_timer(self, cmd, time_str):
        now = datetime.datetime.now()
        schedule_time = datetime.datetime.strptime(time_str,'%H:%M').replace(year=now.year,month=now.month,day=now.day)
        if schedule_time < now:
            schedule_time = schedule_time + datetime.timedelta(days=1)

        time_before_start = int(round((schedule_time-datetime.datetime.now()).total_seconds()))
        print('倒计时:',time_before_start)
        self.schedule.enter(time_before_start, 0, self.execute_command, (cmd,))
        self.schedule.run()

def loop():
    Timer().cmd_timer('unlock','8:53')

if __name__ == "__main__":
    t = threading.Thread(target=loop, name='LoopThread')
    t.start()
    LockScreen().lock()